package com.ygqh.baby.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import com.ygqh.baby.ao.DataStatus;
import com.ygqh.baby.ao.Message;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.mapper.YgCardGiveRecordMapper;
import com.ygqh.baby.model.CardCouponReceiver;
import com.ygqh.baby.po.YgCardGiveRecord;
import com.ygqh.baby.po.YgCardGiveRecordExample;
import com.ygqh.baby.po.YgCardGiveRecordExample.Criteria;
import com.ygqh.baby.service.YgCardGiveRecordService;

@Service
public class YgCardGiveRecordServiceImpl implements YgCardGiveRecordService {

	@Autowired
	private YgCardGiveRecordMapper ygCardGiveRecordMapper;

	@Override
	public List<YgCardGiveRecord> find() {
		YgCardGiveRecordExample example = new YgCardGiveRecordExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygCardGiveRecordMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		YgCardGiveRecordExample example = new YgCardGiveRecordExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygCardGiveRecordMapper.countByExample(example);
	}

	@Override
	public YgCardGiveRecord findById(Long id) {
		return ygCardGiveRecordMapper.selectByPrimaryKey(id);
	}

	@Override
	public int save(YgCardGiveRecord ygCardGiveRecord) {
		return ygCardGiveRecordMapper.insertSelective(ygCardGiveRecord);
	}

	@Override
	public int update(YgCardGiveRecord ygCardGiveRecord) {
		return ygCardGiveRecordMapper.updateByPrimaryKeySelective(ygCardGiveRecord);
	}

	@Override
	public ResultSet<YgCardGiveRecord> search(QueryInfo queryInfo, String q) {
		List<YgCardGiveRecord> list = ygCardGiveRecordMapper.selectSuper(queryInfo, q);
		int count = ygCardGiveRecordMapper.countSuper(q);
		ResultSet<YgCardGiveRecord> resultSet = new ResultSet<YgCardGiveRecord>(count, list);
		return resultSet;
	}

	@Override
	public int deleteSoft(Long[] ids) {
		Assert.notEmpty(ids);
		YgCardGiveRecordExample example = new YgCardGiveRecordExample();
		example.createCriteria().andIdIn(Arrays.asList(ids));
		YgCardGiveRecord record = new YgCardGiveRecord();
		record.setStatus(DataStatus.Delete);

		return ygCardGiveRecordMapper.updateByExampleSelective(record, example);
	}

	@Override
	public int deleteSoft(Long id) {
		return this.deleteSoft(new Long[] { id });
	}

	@Override
	public int saveOrUpdate(YgCardGiveRecord ygCardGiveRecord) {
		int r = 0;
		if (ygCardGiveRecord.getId() != null) {
			r = this.update(ygCardGiveRecord);
		} else {
			r = this.save(ygCardGiveRecord);
		}
		return r;
	}

	@Override
	public int addGiveRecord(Long cardId, Long giver) {
		List<YgCardGiveRecord> list = this.findByGiver(cardId, giver);
		if (CollectionUtils.isEmpty(list)) {
			YgCardGiveRecord cardGiveRecord = new YgCardGiveRecord();
			cardGiveRecord.setCardDetailId(cardId);
			cardGiveRecord.setGiver(giver);
			cardGiveRecord.setGiveTime(new Date());
			return this.save(cardGiveRecord);
		}
		return 0;
	}

	@Override
	public List<YgCardGiveRecord> findByGiver(Long cardId, Long giver) {
		YgCardGiveRecordExample example = new YgCardGiveRecordExample();
		Criteria criteria = example.createCriteria().andGiverEqualTo(giver).andStatusEqualTo(DataStatus.Valid.name());
		if (cardId != null) {
			criteria.andCardDetailIdEqualTo(cardId);
		}
		List<YgCardGiveRecord> list = ygCardGiveRecordMapper.selectByExample(example);
		return list;

	}

	@Override
	public int receiveCard(Long cardId, Long sharer, Long receiver) {
		return ygCardGiveRecordMapper.receiveCard(cardId, sharer, receiver);
	}

	@Override
	public Boolean isReceived(Long cardId, Long sharer) {
		YgCardGiveRecordExample example = new YgCardGiveRecordExample();
		example.createCriteria().andCardDetailIdEqualTo(cardId).andGiverEqualTo(sharer).andReceiverIsNull()
				.andStatusEqualTo(DataStatus.Valid.name());
		int i = ygCardGiveRecordMapper.countByExample(example);
		if (i == 0) {
			return false;
		}
		return false;
	}

	@Override
	public Boolean isByGived(Long cardId) {
		YgCardGiveRecordExample example = new YgCardGiveRecordExample();
		example.createCriteria().andNewCardDetailIdEqualTo(cardId).andStatusEqualTo(DataStatus.Valid.name());
		int i = ygCardGiveRecordMapper.countByExample(example);
		if (i > 0) {
			return true;
		}
		return false;
	}

	@Override
	public Message validateGiveRecord(List<Long> cardDetailIdList, Long receiver) {
		List<YgCardGiveRecord> recordList = findByCardDetailIdList(cardDetailIdList);
		List<YgCardGiveRecord> receivedList = recordList.stream().filter(o -> o.getReceiver() != null && o.getReceiver().compareTo(receiver) == 0)
				.collect(Collectors.toList());
		if (!CollectionUtils.isEmpty(receivedList)) {
			return Message.error("您已领取过该卡券", receivedList.get(0).getNewCardDetailId());
		}
		List<YgCardGiveRecord> unReceivedList = recordList.stream().filter(o -> o.getReceiver() == null).collect(Collectors.toList());
		if (CollectionUtils.isEmpty(unReceivedList)) {
			return Message.error("卡券已被别人全部领取", null);
		}
		return Message.success(unReceivedList);
	}

	@Override
	public List<YgCardGiveRecord> findByCardDetailIdList(List<Long> cardDetailIdList) {
		if (CollectionUtils.isEmpty(cardDetailIdList)) {
			return new ArrayList<>();
		}
		YgCardGiveRecordExample example = new YgCardGiveRecordExample();
		YgCardGiveRecordExample.Criteria criteria = example.createCriteria();
		criteria.andCardDetailIdIn(cardDetailIdList).andStatusEqualTo(DataStatus.Valid.name());
		return ygCardGiveRecordMapper.selectByExample(example);
	}

	@Override
	public int addBatchByCardDetailIdList(List<Long> cardDetailIdList, Long giver) {
		if (CollectionUtils.isEmpty(cardDetailIdList) || giver == null) {
			return 0;
		}
		List<YgCardGiveRecord> list = this.findByCardDetailIdList(cardDetailIdList);
		if (!CollectionUtils.isEmpty(list)) {
			return 0;
		}
		List<YgCardGiveRecord> recordList = new ArrayList<>();
		Date now = new Date();
		for (Long cardDetailId : cardDetailIdList) {
			YgCardGiveRecord record = new YgCardGiveRecord();
			record.setGiver(giver);
			record.setCardDetailId(cardDetailId);
			record.setGiveTime(now);
			recordList.add(record);
		}
		return ygCardGiveRecordMapper.insertBatch(recordList);
	}

	@Override
	public List<CardCouponReceiver> findUserListByCardDetailIdList(List<Long> cardDetailIdList) {
		return ygCardGiveRecordMapper.selectUserListByCardDetailIdList(cardDetailIdList);
	}

}
